import {
    wxfileTobase64
} from '../../../utils/util.js'
import {
    getInsuranceProgramme,
    addRechargeRecord
} from '../../../api/app'
import {
    massMessageRechargeRecordapi
} from '../../api/message'
import user from '../../../utils/user.js'
const app = getApp()
let base64 = ''
Page({

    /**
     * 页面的初始数据
     */
    data: {
        preview: '',
        money: '',
        bankTitle: '',
        bankNum: '',
        bankName: '',
        insureCompanyType: '',
        yearIndex: '0',
        productType: '',
        bankLogo: '',
        backpage: null,
        yearList: [{
            sum: 200,
            num: '2000'
        }, {
            sum: 500,
            num: '5000'
        }, {
            sum: 1000,
            num: '10000'
        }],
        yearIndex: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (wx.getStorageSync('router')) {
            let pageStack = getCurrentPages()
            var i = pageStack.findIndex((ele, index) => {
                return ele.route === wx.getStorageSync('router')
            })
            var backpage = pageStack.length - i - 1
            this.setData({
                backpage: backpage
            })
            console.log(pageStack)
        }
        console.log('user', user)
        console.log(options, 'options')
        this.setData({
            insureCompanyType: user.insureCompanyType,
            bankTitle: user.openBank,
            bankNum: user.bankNo,
            bankName: user.bankName,
            bankLogo: user.bankLogo,
        })
    },
    onUnload() {
        console.log('卸载啦')
        base64 = ''
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    moneyChange(event) {
        this.setData({
            money: event.detail.value
        })
    },
    upload() {
        wx.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            success: (res) => {
                wxfileTobase64(res.tempFilePaths[0]).then(base => {
                    base64 = base
                    this.setData({
                        preview: res.tempFilePaths[0]
                    })
                })
            }
        })
    },
    copy(event) {
        wx.setClipboardData({
            data: event.currentTarget.dataset.copy, // 这个是要复制的数据
        })
    },
    submit() {
        if (!base64) {
            app.toast('请上传图片')
            return
        }
        massMessageRechargeRecordapi({
            payMoney: this.data.yearList[this.data.yearIndex].sum,
            voucherUrl: base64,
            rechargeAmount: this.data.yearList[this.data.yearIndex].num
        }).then((res) => {
            console.log(res);
            wx.removeStorageSync('router')
            if (this.data.backpage != null) {
                wx.navigateBack({
                    delta: this.data.backpage
                })
            }
            wx.redirectTo({
                url: '../message-recharge-success/message-recharge-success?id=' + res,
            })
        })
    },
    preview() {
        wx.previewImage({
            urls: [this.data.preview],
        })
    },
    // 根据参数获取内容
    async getInsuranceProgramme() {
        const result = await getInsuranceProgramme({
            // ele_sign_plan pay_account_plan
            dictType: 'pay_account_plan'
        })
        console.log(result, 'result')
        this.setData({
            yearList: result
        })
    },
    async onAddRechargeRecord() {
        const {
            dictLabel,
            dictValue
        } = this.data.yearList[this.data.yearIndex]
        addRechargeRecord({
            payMoney: dictValue,
            rechargeAmount: dictLabel,
            voucherUrl: base64,
            receiptId: user.receiptId
        }).then(res => {
            console.log(res, 'res续费返回的结果')
            wx.redirectTo({
                url: '/packageInsurance/pages/insurance-gtg-success/insurance-gtg-success?id=' + res + '&type=' + this.data.rechargeType,
            })
        })
    },
    // 切换分
    onYearIndex(event) {
        const {
            index
        } = event.currentTarget.dataset
        this.setData({
            yearIndex: index
        })
    }
})